#ifndef fnt_hevc_helper
#define fnt_hevc_helper

#define EXTENSION_360_VIDEO 0
#define RExt__HIGH_BIT_DEPTH_SUPPORT 1

#include "TLibVideoIO/TVideoIOYuv.h"
#include "TLibCommon/TComList.h"
#include "TLibCommon/TComPicYuv.h"
#include "TLibDecoder/TDecTop.h"
#include "TLibCommon/CommonDef.h"
#include "TLibDecoder/AnnexBread.h"

#include <list>
#include <mutex>
#include <array>


class HevcReader: public ImageReader {
	public:
		HevcReader(const std::string& fn, const std::vector<char*>* bufs, size_t size):
			fs{bufs, size},
			supported{false}
		{
			supported=probe();
		}
		HevcReader(const std::string& fn):
			fs{fn},
			supported{false}
		{
			supported=probe();
		}
		bool close() {
			if (fs.is_open())
				fs.close();
			return true;
		}
	private:
		InputStream fs;

		Bool m_respectDefDispWindow{false};
		bool supported;

		std::mutex mtx;
		using Slice=std::vector<UChar>;

		constexpr static std::size_t MAX_PAR{64};
		struct Pars {
			std::list<InputNALUnit> nalus;
			std::list<Slice> imgs;
			std::size_t width{0}, height{0};
			char padding[128];
		};

		std::array<Pars, MAX_PAR> pars{};

		~HevcReader() override;
		bool getType(CubeType* type) override;
		bool getSizes(int64_t* w, int64_t* h, int64_t* d) override;
		bool readData(char* ptr, int32_t x0, int32_t y0, int32_t z0, int64_t ystride, int64_t zstride) override;
		bool probe();
		bool probe_impl(std::size_t job_idx);

		bool  xWriteOutput      (std::size_t job_idx, Int& m_iPOCLastDisplay, TComList<TComPic*>* pcListPic , UInt tId); ///< write YUV to file
		bool  xFlushOutput      (std::size_t job_idx, Int& m_iPOCLastDisplay, TComList<TComPic*>* pcListPic ); ///< flush all remaining decoded pictures to file
		Bool write(std::size_t job_idx, TComPicYuv* pPicYuvUser, Int confLeft, Int confRight, Int confTop, Int confBottom, ChromaFormat format);
		Bool writePlane(std::size_t job_idx, Pel* src, Bool is16bit, UInt stride444, UInt width444, UInt height444, const ComponentID compID, const ChromaFormat srcFormat, const ChromaFormat fileFormat, const UInt fileBitDepth);
};

#endif
